//
//  StarViewController.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUPUI()
    }
    
    
    func setUPUI(){
        //背景颜色
        view.backgroundColor = UIColor(white: 247/255.0, alpha: 1)
        //        添加控件
        view .addSubview(headimage)
        view .addSubview(messageLabel)
        headimage.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 80, height: 80))
            make.center.equalTo(view)
//            make.top.equalTo(200)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headimage)
            make.top.equalTo(headimage.snp_bottom).offset(15)
        }
    }
    // 2. 在什么时候去执行(应该写在控制器里面的哪个地方) 界面展示出来才去执行动画
    // 因为界面没有展示出来是不需要去执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 更新约束
        headimage.snp_updateConstraints { (make) -> Void in
                        make.centerY.equalTo(view).offset(-200)
//            make.top.equalTo(100)
        }
        
        headimageview()
    }
    
    // 1. 如何执行弹簧的动画
    /**
    1. 持续时间
    2. 延时多少秒之后执行
    3. Damping -> 阻尼 0-1，取值越小，幅度越大
    4. Velocity -> 初始化速度
    5. 动画的选项
    6. 动画执行完成之后调用的闭包
    */
    func headimageview(){
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 透明度动画执行完毕： --> 跳转界面 （切换根控制器）
                        print("动画执行完毕，切换根控制器")
                        
//                        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
//                        appDelegate?.window?.rootViewController = TabbarViewController()

            NSNotificationCenter.defaultCenter().postNotificationName(ChangeRootVCNotification, object: self)
                })
        }
    }
    // MARK: - 懒加载头像   欢迎语
    
    private lazy var headimage: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 40
        imageView.layer.masksToBounds = true
        
        //    加载数据
        let viewmodel = AuthViewModel.sharedViewModel
        //        取得头像地址加载数据
        
        if let avatarImage = AuthViewModel.sharedViewModel.account?.avatar_large {
            imageView.sd_setImageWithURL(NSURL(string: avatarImage), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        return imageView
    }()
    //    加载消息
    private lazy var messageLabel : UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        label.text = "欢迎回来"
        label.alpha = 0
        return label
    }()
    
    
}
